
package webshop.frontend;

import java.util.ArrayList;

import webshop.database.Database;
import webshop.model.*;

/**
 * Should handle all UI, like HTML generation. Perhaps we need some MV or MVC
 * system?
 */
public class Frontend
{
	private Database	database;
	
	public Frontend( Database database )
	{
		this.database = database;
	}
	
	public String loginPage()
	{
		return Renderer.render( "loginpage" );
	}
	
	public String mainPage()
	{
		return Renderer.render( "mainpage" );
	}
	
	public String register()
	{
		return Renderer.render( "registerpage" );
	}
	
	public String allProducts()
	{
		ArrayList<Product> products = database.getAllProducts();
		
		return Renderer.render( "products.all", "products", products );
	}
	
	public String allUsers()
	{
		ArrayList<User> users = database.getAllUsers();
		return Renderer.render( "users.all", "users", users );
	}
	
	public String newUser( String name, String password )
	{
		if ( name.equals( null ) || password.equals( null ) )
			return "Fail, one of the fiels was empty!";
		database.addUser( name, password );
		return "User added!" + System.getProperty( "line.separator" )
				+ "Please return to the main page to login.";
	}
	
	public boolean authUser( String name, String password )
	{
		if ( name.equals( null ) || password.equals( null ) )
			return false;
		else if ( database.authUser( name, password ) )
			return true;
		else
			return false;
	}
	
	public String newProduct()
	{
		return Renderer.render( "products.new" );
	}
	
	public String newProduct( String userName, Product product )
	{
		User user = database.getUser( userName );
		
		if ( product.getName() == null )
			return "FAIL NULL ARGUMENT";
		database.addProduct( user, product );
		return "Product added!";
	}
	
	public String buyProduct( String userName, int productID )
	{
		database.newSale( userName, productID );
		return Renderer.render( "products.bought" );
	}
	
	public String profile( String name )
	{
		User user = database.getUser( name );
		
		ArrayList<Product> sells = database.getUserSells( name );
		ArrayList<Product> bought = database.getUserBought( name );
		
		return Renderer.render( "profile", "user", user, "sells", sells, "bought", bought );
	}
}
